import { _decorator, Component, Prefab, BoxCollider, ITriggerEvent, RigidBody } from 'cc';
import { Bullet } from '../bullet/Bullet';
import { CollisionType } from '../framework/Enum';
import { GameManager } from '../framework/GameManager';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;

// 定义销毁的边界
const OUTOFBOUNCE = 80
@ccclass('EnemyPlane')
export class EnemyPlane extends Component {
    // 敌机移动速度
    @property
    public enemySpeed = 10
    // 敌机子弹发射周期
    @property
    public bulletTimeOut = 3
    // 敌机子弹速度
    @property
    public bulletSpeed = 0
    // 敌机爆炸效果预制
    @property(Prefab)
    public enemyExplodeEffect: Prefab = null
    // 当前发射子弹时间
    private _currCreateBUlletTime = 0
    // 敌机是否射击
    private _isShooting = false
    // 敌机子弹预设
    private _bulletPrefab: Prefab = null
    // 游戏管理节点
    private _gameManager: GameManager = null

    start() {
        this._currCreateBUlletTime = this.bulletTimeOut
    }

    onEnable() {
        // 获取到当前的碰撞组件
        const collider = this.getComponent(BoxCollider)
        // 监听碰撞事件
        collider.on('onTriggerEnter', this._onTriggerEnter, this)
    }

    onDisEnable() {
        // 获取到当前的碰撞组件
        const collider = this.getComponent(BoxCollider)
        // 取消监听碰撞事件
        collider.off('onTriggerEnter', this._onTriggerEnter, this)
    }

    update(deltaTime: number) {
        // 敌机移动逻辑
        const pos = this.node.position
        this.node.setPosition(pos.x, pos.y, pos.z + this.enemySpeed * deltaTime)
        if (this.node.position.z > OUTOFBOUNCE) PoolManager.instantiate().putNode(this.node)

        // 敌机射击逻辑
        this._currCreateBUlletTime += deltaTime
        if (this._isShooting && this._currCreateBUlletTime > this.bulletTimeOut) {
            this.enemyShoot()
            this._currCreateBUlletTime = 0
        }
    }

    // 设置敌机子弹
    public setBullet(isShooting: boolean, bullet: Prefab, speed: number, gameManager: GameManager) {
        this._isShooting = isShooting
        this._bulletPrefab = bullet
        this._gameManager = gameManager
        this.bulletSpeed = -speed
    }

    // 设置敌机游戏管理节点
    public setGameManager(gameManager: GameManager) {
        this._gameManager = gameManager
    }

    // 敌机射击
    public enemyShoot() {
        // 获取子弹场景
        const bulletRoot = this._gameManager.getComponent(GameManager).bulletRoot
        // 实例化子弹预制,这里先默认子弹1
        const bullet = PoolManager.instantiate().getNode(this._bulletPrefab, bulletRoot)
        // 设置子弹的初始位置
        const pos = this.node.position
        bullet.setPosition(pos.x, pos.y, pos.z + 6)
        // 设置子弹速度
        const bulletComp = bullet.getComponent(Bullet)
        bulletComp.bulletSpeed = this.bulletSpeed
        // 设置子弹碰撞分组和掩码
        const colliderComp = bullet.getComponent(RigidBody)
        colliderComp.setGroup(CollisionType.ENEMY_BULLET)
        colliderComp.setMask(CollisionType.PLAYER)
    }

    // 敌机碰撞事件
    private _onTriggerEnter(e: ITriggerEvent) {
        // 获取碰撞物分组
        const collisionGroup = e.otherCollider.getGroup()
        if (collisionGroup === CollisionType.PLAYER) {
            // 玩家飞机
            PoolManager.instantiate().putNode(this.node)
        } else {
            // 玩家子弹
            // 扣血
            PoolManager.instantiate().putNode(this.node)
        }
        this._enemyExplode()
        this._gameManager.addScore(1)
        this._gameManager.audioMain.play('enemyDie')
    }

    // 敌机爆炸
    public _enemyExplode() {
        const effect = PoolManager.instantiate().getNode(this.enemyExplodeEffect, this._gameManager.node)
        const pos = this.node.position
        effect.setPosition(pos)
    }
}

